package sz.com.postoffice.jxyzwgcase.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import sz.com.postoffice.jxyzwgcase.core.BaseResponseEntity;
import sz.com.postoffice.jxyzwgcase.service.IOneClickResponseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@Api(value = "一键响应", tags = "{一键响应API}")
@RequestMapping(value = "/oneClickResponse")
public class OneClickResponseController {

    private static Logger logger = LoggerFactory.getLogger(OneClickResponseController.class);

    @Resource
    private IOneClickResponseService collectionMarketingForCity;

    /**
     * 揽收营销  市级统计
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "市级统计", notes = "市级统计")
    @RequestMapping(value = "/collectionMarketingForCity", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity collectionMarketingForCity(HttpServletRequest request, @RequestBody Map map) throws Exception {

        List<Map> result = collectionMarketingForCity.collectionMarketingForCity(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }


    /**
     * 揽收营销  区县统计
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "区县统计", notes = "区县统计")
    @RequestMapping(value = "/collectionMarketingForCounty", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity collectionMarketingForCounty(HttpServletRequest request, @RequestBody Map map) throws Exception {

        List<Map> result = collectionMarketingForCity.collectionMarketingForCounty(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }

    /**
     * 营业部统计
     *
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "邮件直邮营业部统计", notes = "邮件直邮营业部统计")
    @RequestMapping(value = "/collectionMarketingForDepartment", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity collectionMarketingForDepartment(HttpServletRequest request, @RequestBody Map map) throws Exception {

        List<Map> result = collectionMarketingForCity.collectionMarketingForDepartment(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }


    /**
     * 揽收营销  明细
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "揽收营销明细", notes = "揽收营销明细")
    @RequestMapping(value = "/collectionMarketingList", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity collectionMarketingDetail(HttpServletRequest request, @RequestBody Map map) throws Exception {

        Map result = collectionMarketingForCity.collectionMarketingList(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }


    /**
     * 邮件直查  明细
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "邮件直查明细", notes = "邮件直查明细")
    @RequestMapping(value = "/directMailSearchList", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity directMailSearchDetail(HttpServletRequest request, @RequestBody Map map) throws Exception {

        Map result = collectionMarketingForCity.directMailSearchList(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }


    /**
     * 揽收营销  明细 导出
     *
     * @param request
     * @param response
     * @param
     * @return
     * @throws
     */
    @ApiOperation(value = "揽收营销明细导出", notes = "揽收营销明细导出")
    @RequestMapping(value = "/exportcollectionMarketingListExcel", method = RequestMethod.GET, produces = "application/json")
    public BaseResponseEntity exportcollectionMarketingListExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map map) throws Exception {
        //一、从后台拿数据
        if (null == request || null == response) {
            return BaseResponseEntity.success(1, "导出失败,请联系管理员.", null);
        }
        request.setCharacterEncoding("UTF-8");
        String processDefineName = (String) map.get("processDefineName");
        //获取导出表单的名字
        if (StringUtils.isBlank(processDefineName)) {
            return BaseResponseEntity.success(1, "参数错误！！", null);
        }
        //二、 数据转成excel
        OutputStream out = setOutputStream(response, processDefineName);
        XSSFWorkbook wb = collectionMarketingForCity.exportcollectionMarketingListExcel(map);
        wb.write(out);
        out.close();
        wb.close();

        return null;
    }


    /**
     * 邮件直查 明细 导出
     *
     * @param request
     * @param response
     * @param
     * @return
     * @throws
     */
    @ApiOperation(value = "邮件直查明细导出", notes = "邮件直查明细导出")
    @RequestMapping(value = "/exportdirectMailSearchListExcel", method = RequestMethod.GET, produces = "application/json")
    public BaseResponseEntity exportdirectMailSearchListExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map map) throws Exception {
        if (null == request || null == response) {
            return BaseResponseEntity.success(1, "导出失败,请联系管理员.", null);
        }

        request.setCharacterEncoding("UTF-8");
        String processDefineName = (String) map.get("processDefineName");
        //获取导出表单的名字
        if (StringUtils.isBlank(processDefineName)) {
            return BaseResponseEntity.success(1, "参数错误！！", null);
        }
        //二、 数据转成excel
        OutputStream out = setOutputStream(response, processDefineName);
        XSSFWorkbook wb = collectionMarketingForCity.exportdirectMailSearchListExcel(map);
        wb.write(out);
        out.close();
        wb.close();

        return null;
    }

    /**
     * 根据省编码查询市  区县  营业部
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "根据省编码查询市区县营业部", notes = "根据省编码查询市区县营业部")
    @RequestMapping(value = "/getDepartmentListByCode", method = RequestMethod.POST, produces = "application/json")
    public BaseResponseEntity getDepartmentListByProvinceCode(@RequestBody Map map) throws Exception {

        List<Map> result = collectionMarketingForCity.getDepartmentListByCode(map);

        return BaseResponseEntity.success(0, "查询成功.", result);
    }


    /**
     * 揽收营销  区县统计 导出
     *
     * @param request
     * @param response
     * @param
     * @return
     * @throws
     */
    @ApiOperation(value = "揽收营销区县统计导出", notes = "揽收营销区县统计导出")
    @RequestMapping(value = "/collectionMarketingForCountyListExcel", method = RequestMethod.GET, produces = "application/json")
    public BaseResponseEntity collectionMarketingForCountyListExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map map) throws Exception {
        //一、从后台拿数据
        if (null == request || null == response) {
            return BaseResponseEntity.success(1, "导出失败,请联系管理员.", null);
        }
        request.setCharacterEncoding("UTF-8");
        String processDefineName = (String) map.get("processDefineName");
        //获取导出表单的名字
        if (StringUtils.isBlank(processDefineName)) {
            return BaseResponseEntity.success(1, "参数错误！！", null);
        }
        //二、 数据转成excel
        OutputStream out = setOutputStream(response, processDefineName);
        //判断是哪个级别的导出
        XSSFWorkbook wb = null;
        if (StringUtils.isNotBlank((String) map.get("countyCode"))) {
            if (StringUtils.isNotBlank((String) map.get("empDeptCode"))) {
                wb = collectionMarketingForCity.collectionMarketingForDepartmentListExcel(map);
            } else {
                wb = collectionMarketingForCity.collectionMarketingForCountyListExcel(map);
            }
        } else {
            wb = collectionMarketingForCity.collectionMarketingForCityListExcel(map);
        }

        wb.write(out);
        out.close();
        wb.close();
        return null;
    }

    /**
     * 设置输出流和输出流的参数
     *
     * @param request
     * @param response
     * @param processDefineName
     * @return
     * @throws IOException
     */
    private OutputStream setOutputStream(HttpServletResponse response, String processDefineName) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = processDefineName + ".xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        return response.getOutputStream();
    }

}
